﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

namespace LabMS.Web.Equipment.common
{
    public partial class buyapplist : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GMSQSH");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");

            if (!IsPostBack)
            {
                BuyAppGridView_DataBind();
            }
            StartDateTBX.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            StartDateTBX.Attributes.Add("readOnly", "-1");
            EndDateTBX.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            EndDateTBX.Attributes.Add("readOnly", "-1");
        }

        //绑定GridView
        protected void BuyAppGridView_DataBind()
        {
            LabMS.BLL.BuyApp buyAppBLL = new LabMS.BLL.BuyApp();
            List<LabMS.Model.BuyApp> buyAppModelList = new List<LabMS.Model.BuyApp>();

            string strWhere = GetSearchInfo();
            if (UserType == "teacher")
            {
                strWhere = " AppPerson='" + UserRealName + "'";
            }
            buyAppModelList = buyAppBLL.GetModelList(strWhere);

            BuyAppGridView.DataSource = HandleList(buyAppModelList);
            BuyAppGridView.DataBind();
        }

        protected void BuyAppGV_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("OnDblClick", "window.location.href='./buyappperview.aspx?ID=" + BuyAppGridView.DataKeys[BuyAppGridView.DataKeys.Count-1].Value.ToString() + "'");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }

        //处理GridView列表
        protected List<LabMS.Model.BuyApp> HandleList(List<LabMS.Model.BuyApp> buyAppList)
        {
            for (int i = 0; i < buyAppList.Count; i++)
            {
                buyAppList[i].Type = ChangeType(buyAppList[i].Type);
                buyAppList[i].State = ChangeState(buyAppList[i].State);
            }

            return buyAppList;
        }

        //获取搜索条件
        protected string GetSearchInfo()
        {
            StringBuilder strWhere = new StringBuilder();

            strWhere.Append("1=1");
            if (!string.IsNullOrEmpty(StateDDL.SelectedValue))
            {
                strWhere.Append(" and State='" + StateDDL.SelectedValue + "'");
            }
            if (TypeDDL.SelectedValue != "0")
            {
                strWhere.Append(" and Type='" + TypeDDL.SelectedValue + "'");
            }
            if (!string.IsNullOrEmpty(AppPersonTBX.Text.Trim()))
            {
                strWhere.Append(" and AppPerson='" + AppPersonTBX.Text.Trim() + "'");
            }
            if (!string.IsNullOrEmpty(AppComment.Text.Trim()))
            {
                strWhere.Append(" and AppComment like '%" + AppComment.Text.Trim() + "%'");
            }
            if (!string.IsNullOrEmpty(VerifyComment.Text.Trim()))
            {
                strWhere.Append(" and VerifyComment like '%" + VerifyComment.Text.Trim() + "%'");
            }
            if(!string.IsNullOrEmpty(StartDateTBX.Text.Trim()))
            {
                strWhere.Append(" and AppTime>='"+StartDateTBX.Text.Trim()+"'" );
            }
            if (!string.IsNullOrEmpty(EndDateTBX.Text.Trim()))
            {
                strWhere.Append(" and AppTime<='" + EndDateTBX.Text.Trim() + "'");
            }

            return strWhere.ToString();
        }

        //转换Type
        protected string ChangeType(string strType)
        {
            string TypeName;
            switch (strType)
            {
                case "xiaohaopin":
                    TypeName="消耗品";
                    break;
                case "lowvalue":
                    TypeName = "低值品";
                    break;
                case "yiqi":
                    TypeName = "仪器";
                    break;
                case "guizhongyiqi":
                    TypeName = "贵重仪器";
                    break;
                default:
                    TypeName = "";
                    break;
            }
            return TypeName;
        }

        //转换State
        protected string ChangeState(string strState)
        {
            string StateName;
            switch (strState)
            {
                case "0":
                    StateName = "未申请";
                    break;
                case "1":
                    StateName = "待审中";
                    break;
                case "2":
                    StateName = "已审批[不通过]";
                    break;
                case "3":
                    StateName = "已审批[通过]";
                    break;
                default:
                    StateName = "";
                    break;
            }
            return StateName;
        }

        //分页
        protected void BuyAppGV_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BuyAppGridView.PageIndex = e.NewPageIndex;
            BuyAppGridView_DataBind();
        }


        //删除
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            LabMS.BLL.BuyApp buyAppBLL = new LabMS.BLL.BuyApp();

            int count = BuyAppGridView.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                CheckBox CBTemp = (CheckBox)BuyAppGridView.Rows[i].FindControl("chk");
                if (CBTemp.Checked)
                {
                    try
                    {
                        buyAppBLL.Delete(int.Parse(BuyAppGridView.DataKeys[i].Value.ToString()));
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }

        protected void Search_OnClick(object sender, EventArgs e)
        {
            BuyAppGridView_DataBind();
        }

        //下拉列表框索引改变
        protected void StateDDL_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            BuyAppGridView_DataBind();
        }

    }
}
